using AlohaIM.Interfaces;
using System;
using System.Collections;
using System.IO;
using System.Reflection;

namespace AlohaIM {

	class PluginHandler {
	
		ArrayList plugins = new ArrayList();
		IProtocol plugin;																// FIXME
		
		public PluginHandler() {}
		
		public void FindPlugins(string path) {
			// Iterate through Plugin Directory ...
			foreach (string filename in Directory.GetFiles(path)) {
				FileInfo file = new FileInfo(filename);
				
				// ... for DLL Files
				if (file.Extension.Equals(".dll")) 
					LoadPlugin(filename);
			}
		}
		
		private void LoadPlugin(string filename) {
			// Load Plugin Assembly
			Assembly pluginAssembly = Assembly.LoadFile(filename);
			
			// Instanciate Plugin
			if (pluginAssembly != null) {
				
				foreach (Type pluginType in pluginAssembly.GetTypes()) {
					plugin = Activator.CreateInstance(pluginType) as IProtocol;			// FIXME
					plugins.Add(plugin);
				}
			}
		}
		
		public int Quantity {get {return plugins.Count;}}								// FIXME
		public IProtocol GetPlugin(int i) {return plugins[i] as IProtocol;}				// FIXME
	}
}
